﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities.Dispatcher;
using Common.Utilities.NetworkUtilities;

namespace Workflows.Components.FileManipulation
{
    /// <summary>
    /// 
    /// </summary>
    public partial class FileTaskEditor : Form, IComponentEditor 
    {
        private object _OwnerActivityNode;
        private FileComponent _FileComponent;

        /// <summary>
        /// 
        /// </summary>
        public FileTaskEditor()
        {
            InitializeComponent();
        }

        private void FileTaskEditor_Load(object sender, EventArgs e)
        {
            this.cbo_FileActions.Items.Clear();
            this.cbo_FileActions.SelectedIndex = -1;
            this.cbo_FileActions.Text = "Select file action...";

            for(int i=0;i<=3;i++)
            {
                FileActionType fileAction = (FileActionType) i;
                this.cbo_FileActions.Items.Add(fileAction.ToString());
            }
        }

        private void cbo_FileActions_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(this.cbo_FileActions.SelectedIndex>=0)
            {
                FileActionType action = (FileActionType) this.cbo_FileActions.SelectedIndex;
                switch(action)
                {
                    case FileActionType.Copy:
                        this.lbl_SrcPath.Text = "Source file:";
                        this.lbl_TgtPath.Text = "Target folder:";
                        this.lbl_TgtPath.Visible = true;
                        this.txt_TargetPath.Visible = true;
                        this.btn_Browse2.Visible = true;
                        this.ck_OverWrite.Visible = true;
                        break;
                    case FileActionType.Delete:
                        this.lbl_SrcPath.Text = "Source file:";
                        this.lbl_TgtPath.Visible = false;
                        this.txt_TargetPath.Visible = false;
                        this.btn_Browse2.Visible = false;
                        this.ck_OverWrite.Visible = false;
                        break;
                    case FileActionType.Move:
                        this.lbl_SrcPath.Text = "Source file:";
                        this.lbl_TgtPath.Text = "Target folder:";
                        this.lbl_TgtPath.Visible = true;
                        this.txt_TargetPath.Visible = true;
                        this.btn_Browse2.Visible = true;
                        this.ck_OverWrite.Visible = true;
                        break;
                    case FileActionType.Rename:
                        this.lbl_SrcPath.Text = "Source file:";
                        this.lbl_TgtPath.Visible = true;
                        this.lbl_TgtPath.Text = "New name:";
                        this.txt_TargetPath.Visible = true;
                        this.btn_Browse2.Visible = false;
                        this.ck_OverWrite.Visible = true;
                        break;
                }
            }
        }

        private void btn_Browse1_Click(object sender, EventArgs e)
        {
            if (this.cbo_FileActions.SelectedIndex >= 0)
            {
                FileActionType action = (FileActionType)this.cbo_FileActions.SelectedIndex;
                switch (action)
                {
                    case FileActionType.Copy:
                    case FileActionType.Delete:
                    case FileActionType.Move:
                    case FileActionType.Rename:
                        OpenFileDialog dlg=new OpenFileDialog();
                        dlg.Filter = "All files(*.*)|*.*";
                        if(dlg.ShowDialog()==System.Windows.Forms.DialogResult.OK)
                        {
                            this.txt_SrcPath.Text = NetworkShareMap.getUNCPath(dlg.FileName);
                        }
                        break;
                    default:
                        break;
                }
            }
            else
            {
                this.cbo_FileActions.Focus();
                MessageBox.Show("Select file action type");
            }
        }

        private void btn_Browse2_Click(object sender, EventArgs e)
        {
            if (this.cbo_FileActions.SelectedIndex >= 0)
            {
                FileActionType action = (FileActionType)this.cbo_FileActions.SelectedIndex;
                switch (action)
                {
                    case FileActionType.Copy:
                    case FileActionType.Move:
                        FolderBrowserDialog dlg=new FolderBrowserDialog();
                        if(dlg.ShowDialog()==System.Windows.Forms.DialogResult.OK)
                        {
                            this.txt_TargetPath.Text = NetworkShareMap.getUNCPath(dlg.SelectedPath);
                        }
                        break;
                    default:
                        break;
                }
            }
            else
            {
                this.cbo_FileActions.Focus();
                MessageBox.Show("Select file action type");
            }
        }


        public event UpdateComponentSettings UpdateComponentSettingEvent;

        public object OwnerActivityNode
        {
            get { return _OwnerActivityNode; }
            set { _OwnerActivityNode = value; }
        }

        public Type WorkflowComponentType
        {
            get { return typeof(FileComponent); }
        }

        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents,
                                 object activityNode, WorkflowComponentBase wfComponent)
        {
            this._OwnerActivityNode = activityNode;
            this._FileComponent = (FileComponent) wfComponent;

            this.txt_SrcPath.Text = this._FileComponent.SrcPath;
            this.txt_TargetPath.Text = this._FileComponent.TgtPath;
            this.cbo_FileActions.SelectedIndex = (int) this._FileComponent.ActionType;
            this.ck_OverWrite.Checked = this._FileComponent.OverWrite;
        }

        public void SaveSettings()
        {
            this._FileComponent.SrcPath = this.txt_SrcPath.Text;
            this._FileComponent.TgtPath = this.txt_TargetPath.Text;
            this._FileComponent.ActionType = (FileActionType) this.cbo_FileActions.SelectedIndex;
            this._FileComponent.OverWrite = this.ck_OverWrite.Checked;
        }

        public TestResult Test()
        {
            this.SaveSettings();
            return new TestResult(true,string.Empty);
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if(this.UpdateComponentSettingEvent !=null)
            {
                this.UpdateComponentSettingEvent(this._OwnerActivityNode, this._FileComponent);
            }
            this.Close();
        }
    }
}
